#!/bin/bash
#version---20171018:2053

a="安装设置tomcat（已包含2,3配置步骤）"
#aa="安装设置tomcat（已包含3配置步骤）--"
b="设置环境变量"
c="设置tomcat配置"
d="程序升级目录部署"
e="还原环境变量"
z="退出"
#定义系统环境设置文件、备份文件
local_profile="/etc/profile"
profile_bak="`pwd`/profile_bak"

function jave_set(){
	echo ${local_profile}

	if [ -w ${local_profile} ]; then
		cp -f ${local_profile} ${profile_bak}
		echo "正在清除旧的java配置本地环境变量"
		sed -i '/JAVA_HOME=/d' ${local_profile}
		sed -i '/JAVA_BIN=/d' ${local_profile}
		sed -i '/PATH=$JAVA_BIN:/d' ${local_profile}
		sed -i '/CLASSPATH=$JAVA_HOME/d' ${local_profile}
		sed -i '/CATALINA_HOME=/d' ${local_profile}
		sed -i '/export JAVA_HOME JAVA_BIN/d' ${local_profile}
		echo "开始设置新的java配置本地环境变量"
		sed -i '$a\# We are Setting environment variables for JAVA-9' ${local_profile}
		sed -i '$a\JAVA_HOME=/usr/java/jdk-9' ${local_profile}
		sed -i '$a\JAVA_BIN=/usr/java/jdk-9/bin' ${local_profile}
		sed -i '$a\PATH=$JAVA_BIN:$PATH' ${local_profile}
		sed -i '$a\CLASSPATH=$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar' ${local_profile}
		sed -i '$a\CATALINA_HOME=/usr/local/apache-tomcat-9.0.64' ${local_profile}
		sed -i '$a\export JAVA_HOME JAVA_BIN PATH CLASSPATH' ${local_profile}
		echo "设置成功，开始测试"
		source /etc/profile
		java -version
	else
		echo "Error:write ${local_profile} fialed"
		echo "没有权限修改目标文件：${local_profile}"
		exit
	fi
}

function reduction(){

	cp ${profile_bak} ${lc_profile}
	source /etc/profile
	echo "ok"
}


#定义java-rpm包路径
java_rpm="jdk-9_linux-x64_bin.rpm"

function jave_rpm(){

	if [ ! -f ${java_rpm} ]; then
		echo "没找到jav安装包：${java_rpm}"
		exit
	else
		rpm -ivh ${java_rpm}
	fi
}
#定义tomcat 相关信息
tomcat_gz="apache-tomcat-9.0.64.tar.gz"
tomcat_file="apache-tomcat-9.0.64"
obj__dir="/usr/local"
tomcat_catalina=""

function tomcat_install(){

	if [ ! -f ${tomcat_gz} ]; then
		echo "没找到tomcat安装包：${tomcat_gz}"
		exit
	else
		#echo ${tamcat_gz}
		tar -zxvf ${tomcat_gz}
	fi
	if [ -e ${obj__dir}/${tomcat_file} ]; then
		rm -rf ${obj__dir}/${tomcat_file}
		mv -f ${tomcat_file} ${obj__dir}
		chmod -R 755 ${obj__dir}/${tomcat_file}
	else
		mv -f ${tomcat_file} ${obj__dir}
		chmod -R 755 ${obj__dir}/${tomcat_file}
	fi

}


function tomcat_set(){

	cp -p ${obj__dir}/${tomcat_file}/bin/catalina.sh /etc/init.d/tomcat
	echo "设置tomcat 配置"
		sed -i '1a\# chkconfig: 112 63 37' /etc/init.d/tomcat
		sed -i '2a\# description: tomcat server init script' /etc/init.d/tomcat
		sed -i '3a\# Source Function Library' /etc/init.d/tomcat
		sed -i '4a\ ' /etc/init.d/tomcat
		sed -i '5a\JAVA_HOME=/usr/java/jdk-9' /etc/init.d/tomcat
		sed -i '6a\CATALINA_HOME=/usr/local/apache-tomcat-9.0.642' /etc/init.d/tomcat
	echo "设置tomcat开机启动"
	chmod 755 /etc/init.d/tomcat
	chkconfig --add tomcat
	chkconfig tomcat on
	echo "开始启动tomcat"
	service tomcat start
	echo "操作成功！"
}


function tomcat_http_dir(){
	user="`who am i | awk -F" " '{print $1}'`"
	obj_http_dir="/usr/local/apache-tomcat-9.0.64/webapps/PSM70Update"
	update_dir="/work/PSM70Update"
	update_dir_cfg="/work/PSM70Update/PSM70UpdateConfig"
	echo "正在创建目录!"
	if [ ! -d ${update_dir} ]; then
		mkdir ${update_dir}
		if [ ! -d ${update_dir_cfg} ]; then
		mkdir ${update_dir_cfg}
		fi
	fi
		chown -R ${user} ${update_dir}
		chmod -R 755 ${update_dir}
		
	#echo -n "请输入自定义路径: "
	#read dir_http
	if [ -L ${obj_http_dir} ]; then
		rm -rf ${obj_http_dir}
		ln -sf ${update_dir} ${obj_http_dir}
		echo "目录链接 已创建ok"
	else
		ln -sf ${update_dir} ${obj_http_dir}
		echo "目录链接 已创建ok"
	fi

}


function f_select(){
    PS3=-'请输入对应的数字选择要执行的操作！'
	    select slt in ${a} ${b} ${c} ${d} ${e} ${z}
	do
	    case ${slt} in
	       "${a}")
			jave_rpm;jave_set;tomcat_install;tomcat_set;f_select
		 ;;
	       "${b}")
			jave_set;f_select
		 ;;
	       "${c}")
			tomcat_set;f_select
		 ;;
	       "${d}")
			tomcat_http_dir;f_select
		 ;;
	       "${e}")
			reduction;f_select
		 ;;
	       "${z}")
			echo "bay , bay !"
			exit
		 ;;
		 *)
		echo  "输入错误，请重新选择！" 
		break
		;;
	esac
	done
}
f_select


